package com.ss.controller;

/**
 * @Auther: ljy.s
 * @Date: 2022/9/17 - 09 - 17 - 1:00
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ss.bean.*;
import com.ss.result.R;
import com.ss.service.EstateService;
import com.ss.vo.CellMessage;
import com.ss.vo.UnitMessage;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 房产控制器
 */
@RestController
public class EstateController {

    @Resource
    private EstateService estateService;


    /**
     * 查询所有公司
     *
     * @return 返回所有公司名称给前端
     */
    @RequestMapping("/estate/selectCompany")
    public R selectCompany() {
        System.out.println("selectCompany");
        List<TblCompany> companies = estateService.selectCompany();
        return new R(companies);
    }

    /**
     * 如果出现重复，则不允许插入
     *
     * @return 返回插入结果
     */
    @RequestMapping("/estate/insertEstate")
    public R insertEstate(FcEstate fcEstate) {
        System.out.println("insertEstate");
        Integer result = estateService.insertEstate(fcEstate);
        if (result == 0) {
            return new R("房产编码已经存在");
        }
        return new R(200, result.toString(), "插入成功");// toString,"1"的字符串
    }

    /**
     * 生成数据(楼宇数量，楼宇编码)进行回显
     * 对前端来说是查询，对后端来说是插入数据，进行回显
     *
     * @return 回显的数据
     */
    @RequestMapping("/estate/selectBuilding")
    public R selectBuilding(Integer buildingNumber, String estateCode) {
        System.out.println("selectBuilding");
        List<FcBuilding> fcBuildings = estateService.selectBuilding(buildingNumber, estateCode);
        return new R(fcBuildings);
    }

    /**
     * 根据前端填写的数据，做对应的修改
     *
     * @param fcBuilding 楼宇信息
     * @return 回显修改后的信息
     */
    @RequestMapping("/estate/updateBuilding")
    public R updateBuilding(FcBuilding fcBuilding) {
        System.out.println("updateBuilding");
        Integer result = estateService.updateBuilding(fcBuilding);
        if (result == 1) {
            return new R("更新数据成功");
        }
        return new R("更新数据失败");
    }

    /**
     * 前端传递参数（数组类型）
     * buildingCode:楼宇编码
     * uniCount:单元数量
     * 如果前端传递的参数与后端没有完全匹配的实体类，那么我们需要通过
     * 另外的类型来进行接收，也就是值对象："VO"
     *
     * @return
     */
    @RequestMapping("/estate/selectUnit")
    public R selectUnit(@RequestBody UnitMessage[] unitMessage) {
        System.out.println("selectUnit");
        List<FcUnit> allUnit = new ArrayList<>();
        for (UnitMessage message : unitMessage) {
            allUnit.addAll(estateService.selectUnit(message));
        }
        return new R(allUnit);
    }

    /**
     * 修改单元数据
     *
     * @param fcUnit 单元数据
     * @return 返回是否更新成功
     */
    @RequestMapping("/estate/updateUnit")
    public R updateUnit(FcUnit fcUnit) {
        System.out.println("updateUnit");
        Integer result = estateService.updateUnit(fcUnit);
        if (result == 1) {
            return new R("更新数据成功！");
        }
        return new R("更新数据失败");
    }

    /**
     * 维护房间信息，添加接收的前端信息，添加房间信息
     *
     * @return 是否添加成功
     */
    @RequestMapping("/estate/insertCell")
    public R insertCell(@RequestBody CellMessage[] cellMessages) {
        System.out.println("insertCell");
        List<FcCell> allFcCell = new ArrayList<>();
        for (CellMessage cellMessage : cellMessages) {
            allFcCell.addAll(estateService.insertCell(cellMessage));
        }
        return new R(allFcCell);
    }

    /**
     * 查询对应的楼宇信息
     *
     * @return 返回对应的楼宇信息
     */
    @RequestMapping("/estate/selectBuildingByEstate")
    public R selectBuildingByEstate(String estateCode) {
        System.out.println("selectBuildingByEstate");
        List<FcBuilding> fcBuildings = estateService.selectBuildingByEstate(estateCode);
        return new R(fcBuildings);
    }

    /**
     * 查询对应的单元信息
     *
     * @param buildingCode 单元编码
     * @return 返回对应的单元信息
     */
    @RequestMapping("/estate/selectUnitByBuildingCode")
    public R selectUnitByBuildingCode(String buildingCode) {
        System.out.println("selectUnitByBuildingCode");
        List<FcUnit> fcUnits = estateService.selectUnitByBuildingCode(buildingCode);
        return new R(fcUnits);
    }

    /**
     * 根据单元编码查询对应房间信息
     *
     * @param unitCode 单元编码
     * @return 回显房间信息
     */
    @RequestMapping("/estate/selectCell")
    public R selectCell(String unitCode) {
        System.out.println("selectCell");
        List<FcCell> fcCells = estateService.selectCell(unitCode);
        return new R(fcCells);
    }

    /*@RequestMapping("/estate/selectEstate")
    public R selectEstate(FcBuilding fcBuilding) {
        System.out.println("selectEstate");
        Integer result = estateService.selectEstate(fcBuilding);
        if (result == 1) {
            return new R("更新数据成功！");
        }
        return new R("更新数据失败！");
    }*/

    /**
     * 根据公司查询出对应住宅
     *
     * @param company 公司信息
     * @return 回显住宅信息
     */
    @RequestMapping("/estate/selectEstate")
    public R selectEstate(String company) {
        System.out.println("selectEstate");
        List<FcEstate> fcEstates = estateService.selectEstate(company);
        return new R(fcEstates);
    }

    /**
     * 查询住宅全部信息
     *
     * @return 回显住宅信息
     */
    @RequestMapping("/estate/selectAllEstate")
    public R selectAllEstate() {
        System.out.println("selectAllEstate");
        List<FcEstate> fcEstates = estateService.selectAllEstate();
        return new R(fcEstates);
    }

    /**
     * 此接口可以省略，在前端直接改为之前的selectBuildingByEstate即可
     * 要注意业务层需要做更改，变成查询全部数据
     *
     * @param estateCode
     * @return
     */
    @RequestMapping("/estate/selectBuildingByEstateCode")
    public R selectBuildingByEstateCode(String estateCode) {
        System.out.println("selectBuildingByEstateCode");
        List<FcBuilding> fcBuildings = estateService.selectBuildingByEstateCode(estateCode);
        return new R(fcBuildings);
    }

    @RequestMapping("/estate/selectBuildingByEstateCodeAndBuildingCode")
    public R selectBuildingByEstateCodeAndBuildingCode(@RequestParam("estateCode") String estateCode,
                                                       @RequestParam("buildingCode") String buildingCode) {
        System.out.println("selectBuildingByEstateCodeAndBuildingCode");
        FcBuilding fcBuilding = estateService.selectBuildingByEstateCodeAndBuildingCode(estateCode, buildingCode);
        return new R(fcBuilding);
    }
}
